package com.neusoft.repository.service;

import com.neusoft.repository.entity.AddFormEntity;
import com.neusoft.repository.helper.ServiceResultHelper;
import org.apache.solr.client.solrj.SolrServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @program: repository
 * @description:
 * @author: dong.tl
 * @create: 2019-02-19 18:09
 **/
@Service
public class RepService {

    private static Logger log = LoggerFactory.getLogger(RepService.class);

    @Autowired
    private GridFsService gridFsService;
    @Autowired
    private SolrSerivce solrSerivce;

    public Object saveDoc(AddFormEntity form) {
        try {
            String fileId = gridFsService.saveFile(form.getInputFile());

            solrSerivce.saveDoc(fileId, form);
        } catch (Exception e) {
            log.error("添加文档异常", e);
            return ServiceResultHelper.genResultWithFaild("文档保存失败!");
        }
        return ServiceResultHelper.genResultWithSuccess();
    }

    public Object search(String search_text, int page, int count) {
        try {
            Map<String, Object> search = solrSerivce.search(search_text, page, count);
            return ServiceResultHelper.genResultWithSuccess(search);
        } catch (Exception e) {
            log.error("查询异常", e);
            return ServiceResultHelper.genResultWithFaild("查询失败");
        }
    }

    public Object del(String id, String fileId) {
        try {
            gridFsService.deleteFile(fileId);
            solrSerivce.delete(id);
            return ServiceResultHelper.genResultWithSuccess();
        } catch (Exception e) {
            log.error("删除异常", e);
            return ServiceResultHelper.genResultWithFaild("删除失败");
        }
    }


}
